#
# Copyright 2018 Asylo authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

licenses(["notice"])  # Apache v2.0

# Architecture abstraction layer.
#

package(
    default_visibility = ["//asylo:implementation"],
)

load("//asylo/bazel:copts.bzl", "ASYLO_DEFAULT_COPTS")

# Target exposing trusted architecture-dependent components for the build
# configuration.
cc_library(
    name = "trusted_arch",
    hdrs = [
        "include/trusted/enclave_interface.h",
        "include/trusted/hardware_random.h",
        "include/trusted/host_calls.h",
        "include/trusted/time.h",
    ],
    deps = select({
        "//asylo/platform/arch:sgx": ["trusted_sgx"],
        "//conditions:default": ["trusted_build_only"],
    }) + [
        "//asylo/platform/core:shared_name",
        "//asylo/util:status",
    ],
)

# Target exposing untrusted client components for all backends.
cc_library(
    name = "untrusted_arch",
    hdrs = [
        "sgx/untrusted/sgx_client.h",
    ],
    deps = [
        ":untrusted_sgx",
        "//asylo/platform/core:untrusted_core",
        "//asylo/util:status",
        "@linux_sgx//:common_inc",
    ],
)

# Command to invoke the sgx edger8r tool to generate the bridge code.

ASYLO_PREFIX = "external/com_google_asylo/"

SGX_EDGER8R_COMMAND = (
    "./$(location @linux_sgx//:sgx_edger8r) " +
    "$(location sgx/bridge.edl) " +
    "--search-path " + ASYLO_PREFIX + "asylo/platform/arch/sgx " +
    "--search-path asylo/platform/arch/sgx " +
    "--search-path $(@D)/sgx " +  # Reference previously generated files
    "--trusted --trusted-dir $(@D)/sgx/trusted/ " +
    "--untrusted --untrusted-dir $(@D)/sgx/untrusted/"
)

# Command to update the generated bridge files and header paths for the
# SDK dependencies.

SGX_PREFIX = ""

UPDATE_SDK_DEPS_COMMAND = (
    "for file in $$(ls $(@D)/sgx/*trusted/bridge_*.[ch]); do " +
    "sed -i " +
    "-e '1s!^!// Generated file. See bridge.edl\\n!' " +
    "-e 's!bridge_u.h!generated_bridge_u.h!' " +
    "-e 's!bridge_t.h!generated_bridge_t.h!' " +
    "-e 's!sgx_trts.h!" + SGX_PREFIX + "common/inc/sgx_trts.h!' " +
    "-e 's!sgx_edger8r.h!" + SGX_PREFIX + "common/inc/sgx_edger8r.h!' " +
    "$$file && " +
    "mv $$file $$(dirname $$file)/generated_$$(basename $$file); " +
    "done"
)

# The bridge code generated by the Intel SGX SDK edger8r tool.
genrule(
    name = "generate_bridge",
    srcs = [
        "sgx/bridge.edl",
        "sgx/errno.edl",
        "//asylo/third_party/intel:sgx_tstdc.edl",
        "//asylo/platform/arch/sgx/host_calls_generator:generated_bridge.edl",
    ],
    outs = [
        "sgx/trusted/generated_bridge_t.c",
        "sgx/trusted/generated_bridge_t.h",
        "sgx/untrusted/generated_bridge_u.c",
        "sgx/untrusted/generated_bridge_u.h",
    ],
    cmd = (SGX_EDGER8R_COMMAND + " && " + UPDATE_SDK_DEPS_COMMAND),
    tools = ["@linux_sgx//:sgx_edger8r"],
)

# Untrusted runtime components for SGX
cc_library(
    name = "untrusted_sgx",
    srcs = [
        "sgx/untrusted/generated_bridge_u.c",
        "sgx/untrusted/generated_bridge_u.h",
        "sgx/untrusted/ocalls.cc",
        "sgx/untrusted/sgx_client.cc",
        "sgx/untrusted/sgx_error_space.cc",
        "sgx/untrusted/sgx_error_space.h",
        "//asylo/platform/arch/sgx/host_calls_generator:generated_ocalls.cc",
    ],
    hdrs = [
        "sgx/untrusted/sgx_client.h",
    ],
    copts = ASYLO_DEFAULT_COPTS,
    visibility = ["//visibility:private"],
    deps = [
        "//asylo:enclave_proto_cc",
        "//asylo/platform/common:bridge_proto_serializer",
        "//asylo/platform/common:bridge_types",
        "//asylo/platform/core:shared_name",
        "//asylo/platform/core:untrusted_core",
        "//asylo/util:status",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/strings",
        "@com_google_asylo//asylo/util:logging",
        "@linux_sgx//:common_inc",
        "@linux_sgx//:urts_inc",
    ],
)

# Trusted runtime components for SGX.
cc_library(
    name = "trusted_sgx",
    srcs = [
        "sgx/trusted/ecalls.cc",
        "sgx/trusted/enclave_interface.cc",
        "sgx/trusted/enclave_syscalls.cc",
        "sgx/trusted/exceptions.cc",
        "sgx/trusted/host_calls.cc",
        "sgx/trusted/sbrk.cc",
        "sgx_sim/trusted/hardware_random.cc",
        "//asylo/platform/arch/sgx/host_calls_generator:generated_host_calls.cc",
    ],
    hdrs = [
        "include/trusted/enclave_interface.h",
        "include/trusted/entry_points.h",
        "include/trusted/hardware_random.h",
        "include/trusted/host_calls.h",
    ],
    copts = ["-mrdrnd"],
    linkstatic = 1,
    visibility = ["//visibility:private"],
    deps = [
        ":trusted_sgx_bridge",
        "//asylo:enclave_proto_cc",
        "//asylo/platform/common:bridge_proto_serializer",
        "//asylo/platform/common:bridge_types",
        "//asylo/util:status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/synchronization",
        "@com_google_asylo//asylo/util:logging",
        "@linux_sgx//:common_inc",
    ],
)

# This target supports building trusted targets with the system native compiler,
# which provides a very basic sanity check for Asylo clients in contexts
# where pulling in the cross-compiler toolchain is inconvenient or tricky.
# Attempting to load a dynamic library linked against this target will fail
# since no actual implementation is provided.
cc_library(
    name = "trusted_build_only",
    hdrs = [
        "include/trusted/enclave_interface.h",
        "include/trusted/hardware_random.h",
        "include/trusted/host_calls.h",
    ],
    visibility = ["//visibility:private"],
    deps = ["//asylo/platform/core:shared_name"],
)

# Trusted threading implementation for SGX.
cc_library(
    name = "trusted_sgx_threading",
    srcs = [
        "sgx/trusted/threading.cc",
    ],
    linkstatic = 1,
    visibility = [
        ":__pkg__",
        "//asylo/test:__subpackages__",
    ],
    deps = [
        ":trusted_sgx_bridge",
        "@linux_sgx//:common_inc",
    ],
)

# Trusted side of the SGX SDK generated bridge.
cc_library(
    name = "trusted_sgx_bridge",
    srcs = [
        "sgx/trusted/generated_bridge_t.c",
    ],
    hdrs = [
        "sgx/trusted/generated_bridge_t.h",
    ],
    linkstatic = 1,
    visibility = ["//visibility:private"],
    deps = [
        "//asylo/platform/common:bridge_types",
        "//asylo/platform/core:shared_name",
        "@linux_sgx//:common_inc",
    ],
)

# Set when we are compiling for sgx backend.
config_setting(
    name = "sgx",
    values = {
        "crosstool_top": "@com_google_asylo_sgx_backend//toolchain:crosstool",
    },
)
